﻿#include "EscortMgr.h"
#include "CenterService.h"
#include "Union/UnionMgr.h"
#include "ChatCenter/ChatCenter.h"
//#include "MailCenter/MailManager.h"
#include "Common/TableData/ConstantConstantCfg.h"
#include "Common/TableData/GuildescortQteCfg.h"
#include "Common/TableData/GuildescortPathCfg.h"
#include "Common/Utility/ServerTime.h"
#include "MailCenter/MailManager.h"
#include "ActivityMgr/ActivityMgr.h"

static	uint32_t			_char_times_limit;		//接取上限
static	uint32_t			_guild_times_limit;

EscortMgr::EscortMgr()
{
}

EscortMgr::~EscortMgr()
{
}

bool EscortMgr::Init()
{
	IMessageDispatch *pMessageDispatch = g_GetCenterService()->GetServiceMessageDispatch();
	if (nullptr == pMessageDispatch)
	{
		return false;
	}

	m_pEscortInfoPool = new ObjectPool<Escort>(200, false);
	MMOLOG_PROCESS_ERROR_RET(nullptr != m_pEscortInfoPool, false);

	pMessageDispatch->registerClientHandler(EMOUDLE_ID_ESCORT, this);
	pMessageDispatch->registerServerHandler(EMOUDLE_ID_ESCORT, this);

	g_GetActiveCharacterMgr()->AddCharacterObserver(this);
	g_GetTimerAxis()->SetTimer(ETIMER_ID_ESCORT_UPDATE, 1 * 1000, this);

	mEscortCharIdMap.clear();
	mEscortGuildIdMap.clear();
	mEscortEsctIdMap.clear();

	const ConstantConstantCfgInfo *pEscortGuildTimes = g_GetConstantConstantCfgTable()->GetConstantConstantCfgInfo(EArg_EscortGuildTimes);
	if (pEscortGuildTimes)	_guild_times_limit = pEscortGuildTimes->constantdata;
	const ConstantConstantCfgInfo *pEscortCharTimes = g_GetConstantConstantCfgTable()->GetConstantConstantCfgInfo(EArg_EscortCharTimes);
	if (pEscortCharTimes)	_char_times_limit = pEscortCharTimes->constantdata;
	
	return true;
}

bool EscortMgr::UnInit()
{
	ESCORT_CHARID_MAP::iterator iter1 = mEscortCharIdMap.begin();
	for (; iter1 != mEscortCharIdMap.end(); iter1++)
	{
		Escort *pEscort = iter1->second;
		FreeEscortInfoObj(pEscort);
	}

	ESCORT_GUILDID_MAP::iterator iter2 = mEscortGuildIdMap.begin();
	for (; iter2 != mEscortGuildIdMap.end(); iter2++)
	{
		Escort *pEscort = iter2->second;
		FreeEscortInfoObj(pEscort);
	}

	mEscortCharIdMap.clear();
	mEscortGuildIdMap.clear();
	mEscortEsctIdMap.clear();

	IMessageDispatch *pMessageDispatch = g_GetCenterService()->GetServiceMessageDispatch();
	if (pMessageDispatch)
	{
		pMessageDispatch->unregisterClientHandler(EMOUDLE_ID_ESCORT);
		pMessageDispatch->unregisterServerHandler(EMOUDLE_ID_ESCORT);
	}

	g_GetActiveCharacterMgr()->RemoveCharacterObserver(this);
	g_GetTimerAxis()->KillAllTimer(this);

	MMO_DELETE(m_pEscortInfoPool);

	return true;
}

void EscortMgr::onClientMessage(uint32_t actionId, RECV_PACKAGE &package, uint64_t Id)
{
	switch (actionId)
	{
	case CLIENT_TO_SERVER_ESCORT_RECEIVE:
		HandleReceive(Id, package);
		break;
	case CLIENT_TO_SERVER_ESCORT_QTE_OPERATE:
		HandleQte(Id, package);
		break;
	case CLIENT_TO_SERVER_ESCORT_SPEEDUP:
		HandleSpeedUp(Id, package);
		break;
	case CLIENT_TO_SERVER_ESCORT_REPAIR:
		HandleRepair(Id, package);
		break;
	case CLIENT_TO_SERVER_ESCORT_SUBMIT:
		HandleSubmit(Id, package);
		break;
	case CLIENT_TO_SERVER_ESCORT_GET_INFO:
		HandleGetInfo(Id, package);
		break;
	case CLIENT_TO_SERVER_ESCORT_GET_CHAR_INFO:
		HandleGetCharInfo(Id, package);
		break;
	default:
		break;
	}
}

void EscortMgr::onServerMessage(uint32_t actionId, RECV_PACKAGE &package)
{
	switch (actionId)
	{
	case LOGIC_TO_CENTER_INTERAL_ESCORT_CREATE:
		CreateResult(package);
		break;
	case LOGIC_TO_CENTER_INTERAL_ESCORT_DESTORY:
		DestoryResult(package);
		break;
	case LOGIC_TO_CENTER_INTERAL_ESCORT_SPEEDUP:
		SpeedupResult(package);
		break;
	case LOGIC_TO_CENTER_INTERAL_ESCORT_REPAIR:
		RepairResult(package);
		break;
	case LOGIC_TO_CENTER_INTERAL_ESCORT_GET_INFO:
		GetInfoResult(package);
		break;
	case LOGIC_TO_CENTER_INTERAL_ESCORT_ROBBER:
		OnRobberOff(package);
		break;
	case LOGIC_TO_CENTER_INTERAL_ESCORT_DEAD:
		OnEscortDead(package);
		break;
	case LOGIC_TO_CENTER_INTERAL_ESCORT_TRANS:
		OnTransLogicServer(package);
		break;
	case LOGIC_TO_CENTER_INTERAL_ESCORT_STOPMOVE:
		OnEscortStopMove(package);
		break;
	case LOGIC_TO_CENTER_INTERAL_ESCORT_ADDCONTRI:
		OnEscortAddContri(package);
		break;
	case LOGIC_TO_CENTER_INTERAL_ESCORT_ARRIVAL:
		OnEscortArrival(package);
		break;
	default:
		break;
	}
}

void EscortMgr::OnTimer(uint32_t nTimerID)
{
	switch (nTimerID)
	{
	case ETIMER_ID_ESCORT_UPDATE:
		Update();
		break;
	default:
		break;
	}
}

/*
void EscortMgr::onCharacterLogin(ActiveCharacter *pOnlineCharacter)
{
	CharIDType charId = pOnlineCharacter->GetCharID();
	CharIDType guildId = g_GetUnionMgr()->GetCharUnionId(charId);
	Escort *pEscort = FindEscortByCharId(charId);
	if (nullptr == pEscort)
	{
		pEscort = FindEscortByGuildId(guildId);
	}
	if (nullptr == pEscort)
	{
		return;
	}
	if (pEscort->Type() == EscortType_Char && pEscort->HostID() > 0)
	{
		EscortNotifyInfo notify;
		notify.set_operate(EscortOperate_Add);
		notify.set_escort_cid(pEscort->CID());
		g_GetCenterService()->SendDataToClient(charId, EMOUDLE_ID_ESCORT, SERVER_TO_CLIENT_ESCORT_NOTIFY, &notify);
	}
}*/

void EscortMgr::onCharacterDel(OfflineCharacterData *pOffLineCharacter)
{
	//删号时销毁个人镖
	if (nullptr == pOffLineCharacter)	return;
	CharIDType charId = pOffLineCharacter->GetCharID();
	Escort *pEscort = FindEscortByCharId(charId);
	if (nullptr == pEscort)
	{
		return;
	}

	EscortDestoryReq destory;
	destory.set_escort_cid(pEscort->CID());
	destory.set_destroy_type(EscortOperate_Del_DelRole);
	g_GetCenterService()->SendDataToLogic(pEscort->ServerID(), EMOUDLE_ID_ESCORT, CENTER_TO_LOGIC_INTERAL_ESCORT_DESTORY, &destory);
}

bool EscortMgr::HandleReceive(CharIDType charId, RECV_PACKAGE &package)
{
	EscortReceiveReq req;
	if (!req.ParseFromArray(package.dataAfterHead, package.sizeAfterHead))
	{
		MMOLOG_FMT_ERROR("[center] EscortMgr::HandleReceive EscortReceiveReq parse failed...");
		return false;
	}

	EscortReceiveRsp rsp;
	OfflineCharacterData *pOff = g_GetOfflineCharacterDataMgr()->GetOfflineCharacter(charId);
	if (nullptr == pOff)
	{
		rsp.set_retcode(RET_NOT_HAVE_CHARACTER);
		return g_GetCenterService()->SendDataToClient(charId, EMOUDLE_ID_ESCORT, SERVER_TO_CLIENT_ESCORT_RECEIVE, &rsp);
	}

	ActiveCharacter* pActiveCharacter = g_GetActiveCharacterMgr()->GetOnlineCharacter(charId);
	if (nullptr == pActiveCharacter)
	{
		rsp.set_retcode(RET_PLAYER_NOT_ONLINE);
		return g_GetCenterService()->SendDataToClient(charId, EMOUDLE_ID_ESCORT, SERVER_TO_CLIENT_ESCORT_RECEIVE, &rsp);
	}

	CUnion *pUnion = g_GetUnionMgr()->GetCharUnion(charId);
	if (nullptr == pUnion)
	{
		rsp.set_retcode(RET_UNION_NOT_JOIN);
		return g_GetCenterService()->SendDataToClient(charId, EMOUDLE_ID_ESCORT, SERVER_TO_CLIENT_ESCORT_RECEIVE, &rsp);
	}
/*
	if (g_GetUnionMgr()->GetCharUnionId(charId) <= 0)
	{
		rsp.set_retcode(RET_UNION_NOT_JOIN);
		return g_GetCenterService()->SendDataToClient(charId, EMOUDLE_ID_ESCORT, SERVER_TO_CLIENT_ESCORT_RECEIVE, &rsp);
	}*/

	const GuildescortEscortCfgInfo *pCfg = g_GetGuildescortEscortCfgTable()->GetGuildescortEscortCfgInfo(req.escort_id());
	if (nullptr == pCfg)
	{
		rsp.set_retcode(RET_ESCORT_CONFIG_ERROR);
		return g_GetCenterService()->SendDataToClient(charId, EMOUDLE_ID_ESCORT, SERVER_TO_CLIENT_ESCORT_RECEIVE, &rsp);
	}

	if (pOff->GetLevel() < pCfg->playerLevelLimit)
	{
		rsp.set_retcode(RET_LEVEL_LACK);
		return g_GetCenterService()->SendDataToClient(charId, EMOUDLE_ID_ESCORT, SERVER_TO_CLIENT_ESCORT_RECEIVE, &rsp);
	}

	if (pUnion->BuildLev(EUnionBuildType_Office) < pCfg->escortLevelLimit)
	{
		rsp.set_retcode(RET_ESCORT_LEVEL_LACK);
		return g_GetCenterService()->SendDataToClient(charId, EMOUDLE_ID_ESCORT, SERVER_TO_CLIENT_ESCORT_RECEIVE, &rsp);
	}

	if (FindActiveEscortCId(charId))
	{
		rsp.set_retcode(RET_ESCORT_TASK_EXIST);
		return g_GetCenterService()->SendDataToClient(charId, EMOUDLE_ID_ESCORT, SERVER_TO_CLIENT_ESCORT_RECEIVE, &rsp);
	}

	if (pCfg->teamConvoy == EscortType_Char)
	{
		Receive_Char(pActiveCharacter, req.escort_id(), pCfg, pOff);
	}
	else if (pCfg->teamConvoy == EscortType_Guild)
	{
		Receive_Guild(pActiveCharacter, req.escort_id(), pCfg);
	}
	return true;
}

bool EscortMgr::HandleQte(CharIDType charId, RECV_PACKAGE &package)
{
	EscortQteReq req;
	if (!req.ParseFromArray(package.dataAfterHead, package.sizeAfterHead))
	{
		MMOLOG_FMT_ERROR("[center] EscortMgr::HandleQte EscortReceiveReq parse failed...");
		return false;
	}

	EscortQteRsp rsp;
	rsp.set_retcode(RET_SUCCESS);
	OfflineCharacterData *pOff = g_GetOfflineCharacterDataMgr()->GetOfflineCharacter(charId);
	if (nullptr == pOff)
	{
		rsp.set_retcode(RET_NOT_HAVE_CHARACTER);
		return g_GetCenterService()->SendDataToClient(charId, EMOUDLE_ID_ESCORT, SERVER_TO_CLIENT_ESCORT_QTE_OPERATE, &rsp);
	}

	ActiveCharacter* pActiveCharacter = g_GetActiveCharacterMgr()->GetOnlineCharacter(charId);
	if (nullptr == pActiveCharacter)
	{
		rsp.set_retcode(RET_PLAYER_NOT_ONLINE);
		return g_GetCenterService()->SendDataToClient(charId, EMOUDLE_ID_ESCORT, SERVER_TO_CLIENT_ESCORT_QTE_OPERATE, &rsp);
	}

	Escort *pEscort = FindActiveEscortCId(charId);
	if (nullptr == pEscort || pEscort->CID() <= 0)
	{
		rsp.set_retcode(RET_ESCORT_TASK_NOT_EXIST);
		return g_GetCenterService()->SendDataToClient(charId, EMOUDLE_ID_ESCORT, SERVER_TO_CLIENT_ESCORT_QTE_OPERATE, &rsp);
	}

	if (1 == req.operate())			//请求打开QTE
	{
		if (EscortRobberStatus_Active == pEscort->RobberStatus())
		{
			rsp.set_retcode(RET_ESCORT_ROBBER_ING);
			return g_GetCenterService()->SendDataToClient(charId, EMOUDLE_ID_ESCORT, SERVER_TO_CLIENT_ESCORT_QTE_OPERATE, &rsp);
		}
		pEscort->SpeedStatus(EscortSpeedStatus_Active);
		return g_GetCenterService()->SendDataToClient(charId, EMOUDLE_ID_ESCORT, SERVER_TO_CLIENT_ESCORT_QTE_OPERATE, &rsp);
	}
	else if (2 == req.operate())	//关闭QTE
	{
		pEscort->SpeedStatus(EscortSpeedStatus_None);
		if (EscortRobberStatus_Ready == pEscort->RobberStatus())
		{
			pEscort->RobberStatus(EscortRobberStatus_Active);
			C2L_EscortCreateRobber robber;
			robber.set_escort_cid(pEscort->CID());
			robber.set_robber_id(pEscort->RobberID());
			robber.set_robber_level(pEscort->EscortLevel());
			g_GetCenterService()->SendDataToLogic(pEscort->ServerID(), EMOUDLE_ID_ESCORT, CENTER_TO_LOGIC_INTERAL_ESCORT_ROBBER, &robber);

			EscortRobberNotify notify;
			notify.set_escort_cid(pEscort->CID());
			notify.set_status(1);	//1.劫镖进行 2.劫镖停止
			g_GetCenterService()->SendDataToClient(charId, EMOUDLE_ID_ESCORT, SERVER_TO_CLIENT_ESCORT_ROBBER_STATUS, &notify);
		}
		return g_GetCenterService()->SendDataToClient(charId, EMOUDLE_ID_ESCORT, SERVER_TO_CLIENT_ESCORT_QTE_OPERATE, &rsp);
	}
	return true;
}

inline uint32_t escort_path_id(const GuildescortEscortCfgInfo *pCfg)
{
	size_t size = pCfg->vec_relatedPathId.size();
	if (size <= 0)	return 0;
	VEC_INT32 tempVec;
	for (size_t i = 0; i < size; i++)
	{
		if (pCfg->vec_relatedPathId[i] <= 0)
			break;
		tempVec.push_back(pCfg->vec_relatedPathId[i]);
	}
	size_t temp_size = tempVec.size();
	if (temp_size <= 0)	return 0;
	return tempVec[Random(0, temp_size - 1)];
}

void EscortMgr::Receive_Char(ActiveCharacter* pActiveCharacter, uint32_t configId, const GuildescortEscortCfgInfo *pCfg, OfflineCharacterData *pOff)
{
	ERetCode retcode = RET_SUCCESS;
	Escort *pEscort = MulEscortFromCharMap(pActiveCharacter->GetCharID());
	UnionChar *pCharUnion = g_GetUnionMgr()->CharUnionData(pActiveCharacter->GetCharID());
	if (nullptr == pEscort)
	{
		retcode = RET_ESCORT_MALLOC_OBJECT_ERROR;
	}
	else if (nullptr == pCharUnion)
	{
		retcode = RET_UNION_NOT_JOIN;
	}
	else if (pCharUnion->EscortTimes() >= pEscort->CfgTimes())
	{
		retcode = RET_ESCORT_TASK_TIMES_LIMIT;
	}
	else if (pEscort->CID() > 0)
	{
		retcode = RET_ESCORT_TASK_EXIST;
	}

	if (retcode == RET_SUCCESS)
	{
		pEscort->EscortLevel(pOff->GetLevel());
		pEscort->LineID(escort_path_id(pCfg));
		LogInfoFmtPrint("[center] EscortMgr::Receive_Char++++++++++++++++++++++++++++++++++++++++++++++++++pathId=%d", pEscort->LineID());
		EscortCreateReq create;
		create.set_char_id(pActiveCharacter->GetCharID());
		create.set_escort_type(EscortType_Char);
		create.set_escort_level(pEscort->EscortLevel());
		create.set_escort_hostid(pActiveCharacter->GetCharID());
		create.set_escort_configid(configId);
		create.set_escort_monster_id(pCfg->relatedMonsterId);
		create.set_escort_line_id(pEscort->LineID());
		create.set_name(pOff->GetCharName());
		g_GetCenterService()->SendDataToLogicByCharID(pActiveCharacter->GetCharID(), EMOUDLE_ID_ESCORT, CENTER_TO_LOGIC_INTERAL_ESCORT_CREATE, &create);
	}
	else
	{
		EscortReceiveRsp rsp;
		rsp.set_retcode(retcode);
		g_GetCenterService()->SendDataToClient(pActiveCharacter->GetCharID(), EMOUDLE_ID_ESCORT, SERVER_TO_CLIENT_ESCORT_RECEIVE, &rsp);
	}
}

void EscortMgr::Receive_Guild(ActiveCharacter* pActiveCharacter, uint32_t configId, const GuildescortEscortCfgInfo *pCfg)
{
	ERetCode retcode = RET_SUCCESS;
	CUnion *pUnion = g_GetUnionMgr()->GetCharUnion(pActiveCharacter->GetCharID());
	if (nullptr == pUnion)
	{
		retcode = RET_UNION_NOT_JOIN;
	}
	else if (pUnion->WarnLine())
	{
		retcode = RET_UNION_MONEY_IN_WARN_LINE;
	}
	else if (pUnion->LeaderId() != pActiveCharacter->GetCharID())
	{
		retcode = RET_UNION_NOT_LEADER;
	}
	else if (!g_GetActivityMgr()->IsActivityRunning(ESCORT_ACTIVITY_ID))
	{
		retcode = RET_ACTIVITY_NOT_OPEN;
	}
	else if (pUnion->Money() < (uint64_t)pCfg->foregift)
	{
		retcode = RET_UNION_MONEY_LACK;
	}
	else if (GuilderHasEscort(pUnion->UnionId()))
	{
		retcode = RET_ESCORT_GUILDER_TASK_EXIST;
	}

	Escort *pEscort = nullptr;
	if (retcode == RET_SUCCESS)
	{
		pEscort = MulEscortFromGuildMap(pUnion->UnionId());
		if (pEscort == nullptr)
		{
			retcode = RET_ESCORT_MALLOC_OBJECT_ERROR;
		}
		else if (pEscort->CID() > 0)
		{
			retcode = RET_ESCORT_TASK_EXIST;
		}
		else if (pUnion->EscortTimes() >= pEscort->CfgTimes())
		{
			retcode = RET_ESCORT_TASK_TIMES_LIMIT;
		}
	}
	
	if (retcode == RET_SUCCESS)
	{
		pEscort->EscortLevel(pUnion->EliteAverageLev());
		pEscort->LineID(escort_path_id(pCfg));
		LogInfoFmtPrint("[center] EscortMgr::Receive_Guild++++++++++++++++++++++++++++++++++++++++++++++++++pathId=%d", pEscort->LineID());
		EscortCreateReq create;
		create.set_char_id(pActiveCharacter->GetCharID());
		create.set_escort_type(EscortType_Guild);
		create.set_escort_level(pEscort->EscortLevel());
		create.set_escort_hostid(pUnion->UnionId());
		create.set_escort_configid(configId);
		create.set_escort_monster_id(pCfg->relatedMonsterId);
		create.set_escort_line_id(pEscort->LineID());
		create.set_camp(pUnion->CampId());
		create.set_name(pUnion->Name());
		g_GetCenterService()->SendDataToLogicByCharID(pActiveCharacter->GetCharID(), EMOUDLE_ID_ESCORT, CENTER_TO_LOGIC_INTERAL_ESCORT_CREATE, &create);
	}
	else
	{
		EscortReceiveRsp rsp;
		rsp.set_retcode(retcode);
		g_GetCenterService()->SendDataToClient(pActiveCharacter->GetCharID(), EMOUDLE_ID_ESCORT, SERVER_TO_CLIENT_ESCORT_RECEIVE, &rsp);
	}
}

void EscortMgr::HandleSubmit(CharIDType charId, RECV_PACKAGE &package)
{
	EscortSubmitReq req;
	if (!req.ParseFromArray(package.dataAfterHead, package.sizeAfterHead))
	{
		MMOLOG_FMT_ERROR("[center] EscortMgr::Submit EscortSubmitReq parse failed...");
		return;
	}

	ActiveCharacter* pActiveCharacter = g_GetActiveCharacterMgr()->GetOnlineCharacter(charId);

	ERetCode retcode = RET_SUCCESS;
	Escort *pEscort = FindActiveEscortCId(charId);

	if (nullptr == pActiveCharacter)
	{
		retcode = RET_PLAYER_NOT_ONLINE;
	}
	else if (nullptr == pEscort)
	{
		retcode = RET_ESCORT_TASK_NOT_EXIST;
	}
	else if (g_GetUnionMgr()->GetCharUnionId(charId) <= 0)
	{
		retcode = RET_UNION_NOT_JOIN;
	}
/*
	else if (pEscort->Status() != EscortStatus_Complete)
	{
		retcode = RET_ESCORT_TASK_NOT_FINISH;
	}*/

	if (retcode == RET_SUCCESS)
	{
		EscortDestoryReq destory;
		destory.set_char_id(charId);
		destory.set_escort_cid(pEscort->CID());
		destory.set_destroy_type(EscortOperate_Del_TaskSubmit);
		g_GetCenterService()->SendDataToLogicByCharID(charId, EMOUDLE_ID_ESCORT, CENTER_TO_LOGIC_INTERAL_ESCORT_DESTORY, &destory);
	}
	else
	{
		EscortSubmitRsp submitRsp;
		submitRsp.set_retcode(retcode);
		g_GetCenterService()->SendDataToClient(charId, EMOUDLE_ID_ESCORT, SERVER_TO_CLIENT_ESCORT_SUBMIT, &submitRsp);
	}
}

bool EscortMgr::HandleSpeedUp(CharIDType charId, RECV_PACKAGE &package)
{
	EscortSpeedupReq req;
	if (!req.ParseFromArray(package.dataAfterHead, package.sizeAfterHead))
	{
		MMOLOG_FMT_ERROR("[center] EscortMgr::HandleSpeedUp EscortSpeedupReq parse failed...");
		return false;
	}

	EscortSpeedupRsp rsp;
	rsp.set_retcode(RET_SUCCESS);

	ActiveCharacter* pActiveCharacter = g_GetActiveCharacterMgr()->GetOnlineCharacter(charId);
	if (nullptr == pActiveCharacter)
	{
		rsp.set_retcode(RET_PLAYER_NOT_ONLINE);
		return g_GetCenterService()->SendDataToClient(charId, EMOUDLE_ID_ESCORT, SERVER_TO_CLIENT_ESCORT_SPEEDUP, &rsp);
	}

	Escort *pEscort = FindActiveEscortCId(charId);
	if (nullptr == pEscort || pEscort->CID() <= 0)
	{
		rsp.set_retcode(RET_ESCORT_TASK_NOT_EXIST);
		return g_GetCenterService()->SendDataToClient(charId, EMOUDLE_ID_ESCORT, SERVER_TO_CLIENT_ESCORT_SPEEDUP, &rsp);
	}

	if (pEscort->Type() == EscortType_Guild)
	{
		rsp.set_retcode(RET_ESCORT_GUILD_SPEEDUP_ERROR);
		return g_GetCenterService()->SendDataToClient(charId, EMOUDLE_ID_ESCORT, SERVER_TO_CLIENT_ESCORT_SPEEDUP, &rsp);
	}

	const GuildescortQteCfgInfo *qteCfg = g_GetGuildescortQteCfgTable()->GetGuildescortQteCfgInfo(480300001/*需定义成常量*/);
	if (nullptr == qteCfg || qteCfg->vecGuildescortQteButtonNumCfg.size() < (req.index()+1))
	{
		rsp.set_retcode(RET_ESCORT_CONFIG_ERROR);
		return g_GetCenterService()->SendDataToClient(charId, EMOUDLE_ID_ESCORT, SERVER_TO_CLIENT_ESCORT_SPEEDUP, &rsp);
	}
	g_GetCenterService()->SendDataToClient(charId, EMOUDLE_ID_ESCORT, SERVER_TO_CLIENT_ESCORT_SPEEDUP, &rsp);

	pEscort->SpeedTime(qteCfg->vecGuildescortQteButtonNumCfg[req.index()].time);
	float cur_speed = pEscort->Speed() * qteCfg->vecGuildescortQteButtonNumCfg[req.index()].accelerate / 100;
	EscortSpeedup speedup;
	speedup.set_escort_cid(pEscort->CID());
	speedup.set_speed_value(cur_speed);
	return g_GetCenterService()->SendDataToLogic(pEscort->ServerID(), EMOUDLE_ID_ESCORT, CENTER_TO_LOGIC_INTERAL_ESCORT_SPEEDUP, &speedup);
}

bool EscortMgr::HandleRepair(CharIDType charId, RECV_PACKAGE &package)
{
	EscortRepairReq req;
	if (!req.ParseFromArray(package.dataAfterHead, package.sizeAfterHead))
	{
		MMOLOG_FMT_ERROR("[center] EscortMgr::HandleRepair EscortRepairReq parse failed...");
		return false;
	}

	EscortRepairRsp rsp;

	ActiveCharacter* pActiveCharacter = g_GetActiveCharacterMgr()->GetOnlineCharacter(charId);
	if (nullptr == pActiveCharacter)
	{
		rsp.set_retcode(RET_PLAYER_NOT_ONLINE);
		return g_GetCenterService()->SendDataToClient(charId, EMOUDLE_ID_ESCORT, SERVER_TO_CLIENT_ESCORT_REPAIR, &rsp);
	}

	Escort *pEscort = FindActiveEscortCId(charId);
	if (nullptr == pEscort)
	{
		rsp.set_retcode(RET_ESCORT_TASK_NOT_EXIST);
		return g_GetCenterService()->SendDataToClient(charId, EMOUDLE_ID_ESCORT, SERVER_TO_CLIENT_ESCORT_REPAIR, &rsp);
	}

	const GuildescortEscortCfgInfo *pCfg = g_GetGuildescortEscortCfgTable()->GetGuildescortEscortCfgInfo(pEscort->EscortID());
	if (nullptr == pCfg || pCfg->vecGuildescortEscortFixCfg.size() <= 0)
	{
		rsp.set_retcode(RET_ESCORT_CONFIG_ERROR);
		return g_GetCenterService()->SendDataToClient(charId, EMOUDLE_ID_ESCORT, SERVER_TO_CLIENT_ESCORT_REPAIR, &rsp);
	}

	if (pEscort->Type() == EscortType_Char)
	{
		rsp.set_retcode(RET_ESCORT_CHAR_REPAIR_ERROR);
		return g_GetCenterService()->SendDataToClient(charId, EMOUDLE_ID_ESCORT, SERVER_TO_CLIENT_ESCORT_REPAIR, &rsp);
	}
	else if (pEscort->HaveRepair(charId))
	{
		rsp.set_retcode(RET_ESCORT_ALREADY_REPAIR);
		return g_GetCenterService()->SendDataToClient(charId, EMOUDLE_ID_ESCORT, SERVER_TO_CLIENT_ESCORT_REPAIR, &rsp);
	}
	else if (req.repair_index() > (pCfg->vecGuildescortEscortFixCfg.size() - 1))
	{
		MMOLOG_FMT_ERROR("[center] EscortMgr::HandleRepair repair_index error.. failed...");
		rsp.set_retcode(RET_ESCORT_CONFIG_ERROR);
		return g_GetCenterService()->SendDataToClient(charId, EMOUDLE_ID_ESCORT, SERVER_TO_CLIENT_ESCORT_REPAIR, &rsp);
	}

	EscortResumeHpReq resume;
	resume.set_char_id(charId);
	resume.set_escort_cid(pEscort->CID());
	resume.set_escort_id(pEscort->EscortID());
	resume.set_repair_index(req.repair_index());
	return g_GetCenterService()->SendDataToLogicByCharID(charId, EMOUDLE_ID_ESCORT, CENTER_TO_LOGIC_INTERAL_ESCORT_REPAIR, &resume);
}

void EscortMgr::HandleGetInfo(CharIDType charId, RECV_PACKAGE &package)
{
	EscortInfoReq req;
	if (!req.ParseFromArray(package.dataAfterHead, package.sizeAfterHead))
	{
		MMOLOG_FMT_ERROR("[center] EscortMgr::HandleGetInfo EscortPosReq parse failed...");
		return;
	}

	Escort *pEscort = FindActiveEscortCId(charId);
	if (pEscort && pEscort->CID() > 0)
	{
		C2L_EscortInfoReq posreq;
		posreq.set_char_id(charId);
		posreq.set_escort_cid(pEscort->CID());
		posreq.set_drive(EscortDrive_GetInfo);
		g_GetCenterService()->SendDataToLogic(pEscort->ServerID()/*镖车所在的逻辑服ID*/, EMOUDLE_ID_ESCORT, CENTER_TO_LOGIC_INTERAL_ESCORT_GET_INFO, &posreq);
	}
	else
	{
		EscortInfoRsp rsp;
		rsp.set_retcode(RET_ESCORT_TASK_NOT_EXIST);
		g_GetCenterService()->SendDataToClient(charId, EMOUDLE_ID_ESCORT, SERVER_TO_CLIENT_ESCORT_GET_INFO, &rsp);
	}
}

uint64_t EscortMgr::GetStartTick(Escort *e)
{
	if (nullptr == e)
		return 0;

	if (e->Type() == EscortType_Char)
	{
		UnionChar *pCharUnion = g_GetUnionMgr()->CharUnionData(e->HostID());
		if (nullptr != pCharUnion)
			return pCharUnion->LastRcvTime();
	}
	else if (e->Type() == EscortType_Guild)
	{
		CUnion *pUnion = g_GetUnionMgr()->GetUnion(e->HostID());
		if (nullptr != pUnion)
			return pUnion->RcvTime();
	}
	return 0;
}

void EscortMgr::HandleGetCharInfo(CharIDType charId, RECV_PACKAGE &package)
{
	EscortCharInfoReq req;
	if (!req.ParseFromArray(package.dataAfterHead, package.sizeAfterHead))
	{
		MMOLOG_FMT_ERROR("[center] EscortMgr::HandleGetCharInfo EscortCharInfoReq parse failed...");
		return;
	}

	EscortCharInfoRsp rsp;
	rsp.set_retcode(RET_SUCCESS);
	Escort *pEscort = FindActiveEscortCId(charId);
	if (nullptr == pEscort)
	{
		rsp.set_char_times(GetCharEscortTimes(charId));
		rsp.set_union_times(GetGuildEscortTimes(charId));
		g_GetCenterService()->SendDataToClient(charId, EMOUDLE_ID_ESCORT, SERVER_TO_CLIENT_ESCORT_GET_CHAR_INFO, &rsp);
	}
	else
	{
		C2L_EscortInfoReq posreq;
		posreq.set_char_id(charId);
		posreq.set_escort_cid(pEscort->CID());
		posreq.set_drive(EscortDrive_GetCharInfo);
		g_GetCenterService()->SendDataToLogic(pEscort->ServerID()/*镖车所在的逻辑服ID*/, EMOUDLE_ID_ESCORT, CENTER_TO_LOGIC_INTERAL_ESCORT_GET_INFO, &posreq);
	}
}

bool EscortMgr::CreateResult(RECV_PACKAGE &package)
{
	EscortCreateRsp createRsp;
	if (!createRsp.ParseFromArray(package.dataAfterHead, package.sizeAfterHead))
	{
		LogErrFmtPrint("[center] EscortMgr::CreateResult parse failed...");
		return false;
	}

	EscortReceiveRsp receiveRsp;
	if (RET_SUCCESS != createRsp.retcode())
	{
		receiveRsp.set_retcode(createRsp.retcode());
		return g_GetCenterService()->SendDataToClient(createRsp.char_id(), EMOUDLE_ID_ESCORT, SERVER_TO_CLIENT_ESCORT_RECEIVE, &receiveRsp);
	}
	LogInfoFmtPrint("CreateResult...cid:%lu,escortid:%u ,type:%d,escortcid:%lu,map:%u,speed:%f,pos:%f,%f,%f ", createRsp.char_id(), createRsp.escort_configid(), createRsp.escort_type(), createRsp.escort_cid(),createRsp.speed(),createRsp.posx(),createRsp.posy(),createRsp.posz());
	//
	ActiveCharacter* pActiveCharacter = g_GetActiveCharacterMgr()->GetOnlineCharacter(createRsp.char_id());
	if (nullptr == pActiveCharacter)
	{
		receiveRsp.set_retcode(RET_PLAYER_NOT_ONLINE);
		return g_GetCenterService()->SendDataToClient(createRsp.char_id(), EMOUDLE_ID_ESCORT, SERVER_TO_CLIENT_ESCORT_RECEIVE, &receiveRsp);
	}

	const GuildescortEscortCfgInfo *pCfg = g_GetGuildescortEscortCfgTable()->GetGuildescortEscortCfgInfo(createRsp.escort_configid());
	if (nullptr == pCfg)
	{
		receiveRsp.set_retcode(RET_ESCORT_CONFIG_ERROR);
		return g_GetCenterService()->SendDataToClient(createRsp.char_id(), EMOUDLE_ID_ESCORT, SERVER_TO_CLIENT_ESCORT_RECEIVE, &receiveRsp);
	}

	Escort *pEscort = nullptr;
	uint64_t curTime = g_GetGlobalServerTime()->UnixSec();
	if (createRsp.escort_type() == EscortType_Char)
	{
		pEscort = FindEscortByCharId(createRsp.char_id());
		if (nullptr == pEscort)
		{
			receiveRsp.set_retcode(RET_ESCORT_MALLOC_OBJECT_ERROR);
			return g_GetCenterService()->SendDataToClient(createRsp.char_id(), EMOUDLE_ID_ESCORT, SERVER_TO_CLIENT_ESCORT_RECEIVE, &receiveRsp);
		}
		pEscort->Init(createRsp.escort_configid(), createRsp.char_id(), createRsp.escort_cid());
		mEscortEsctIdMap[createRsp.escort_cid()] = pEscort;

		UnionChar *pCharUnion = g_GetUnionMgr()->CharUnionData(createRsp.char_id());
		if (pCharUnion)
		{
			pCharUnion->LastRcvTime(curTime);
			pCharUnion->EscortTimes(pCharUnion->EscortTimes() + 1);
		}
		else
			LogErrFmtPrint("[center] EscortMgr::CreateResult pCharUnion = nullptr...charId:%llu", createRsp.char_id());
	}
	else/* if (createRsp.escort_type() == EscortType_Guild)*/
	{
		CharIDType guildId = g_GetUnionMgr()->GetCharUnionId(createRsp.char_id());
		pEscort = FindEscortByGuildId(guildId);
		if (nullptr == pEscort)
		{
			receiveRsp.set_retcode(RET_ESCORT_MALLOC_OBJECT_ERROR);
			return g_GetCenterService()->SendDataToClient(createRsp.char_id(), EMOUDLE_ID_ESCORT, SERVER_TO_CLIENT_ESCORT_RECEIVE, &receiveRsp);
		}
		pEscort->Init(createRsp.escort_configid(), guildId, createRsp.escort_cid());
		mEscortEsctIdMap[createRsp.escort_cid()] = pEscort;
		g_GetUnionMgr()->AddUnionMoney(createRsp.char_id(), -pCfg->foregift);

		CUnion *pUnion = g_GetUnionMgr()->GetUnion(guildId);
		if (pUnion)
		{
			pUnion->RcvTime(curTime);
			//pUnion->EscortTimes(pUnion->EscortTimes() + 1);	//完成后加次数
		}
		else
			LogErrFmtPrint("[center] EscortMgr::CreateResult pUnion = nullptr...guildId:%llu", guildId);
	}
	pEscort->ServerID(package.session->GetUserData().v32);
	pEscort->Speed(createRsp.speed());

	EscortData eData;
	eData.set_escort_cid(pEscort->CID());
	eData.set_escort_id(pEscort->EscortID());
	eData.set_path_id(pEscort->LineID());
	eData.set_repair_flag(pEscort->HaveRepair(createRsp.char_id()));
	eData.set_time_tick(curTime);

	eData.set_map_id(createRsp.map_id());
	eData.set_posx(createRsp.posx());
	eData.set_posy(createRsp.posy());
	eData.set_posz(createRsp.posz());
	receiveRsp.mutable_escort_data()->CopyFrom(eData);
	receiveRsp.set_retcode(RET_SUCCESS);
	g_GetCenterService()->SendDataToClient(pActiveCharacter->GetCharID(), EMOUDLE_ID_ESCORT, SERVER_TO_CLIENT_ESCORT_RECEIVE, &receiveRsp);

	if (EscortType_Guild == pEscort->Type())
	{
		SET_UINT64 iSet;
		g_GetUnionMgr()->GetUnionAllMember(pEscort->HostID(), iSet);
		for (SET_UINT64::iterator iter = iSet.begin(); iter != iSet.end(); iter++)
		{
			EscortNotifyInfo notify;
			notify.set_operate(EscortOperate_Add);
			notify.set_escort_cid(pEscort->CID());
			notify.mutable_escort_data()->CopyFrom(eData);
			g_GetCenterService()->SendDataToClient(*iter, EMOUDLE_ID_ESCORT, SERVER_TO_CLIENT_ESCORT_NOTIFY, &notify);
		}

		const GuildescortPathCfgInfo *pPathCfg = g_GetGuildescortPathCfgTable()->GetGuildescortPathCfgInfo(pEscort->LineID());
		if (nullptr == pPathCfg || pPathCfg->vecGuildescortPathRelatedPathCfg.size() <= 0)
		{
			LogErrFmtPrint("[logic] LogicEscortModule::CenterToLogic_CreateEscort GuildescortPathCfgInfo *pPathCfg = error....escort_line_id=%d", pEscort->LineID());
			return false;
		}

		size_t size = pPathCfg->vecGuildescortPathRelatedPathCfg.size();
		VEC_INT32 tempVec;
		for (size_t i = 0; i < size; i++)
		{
			if (pPathCfg->vecGuildescortPathRelatedPathCfg[i].MapId > 0)
				tempVec.push_back(pPathCfg->vecGuildescortPathRelatedPathCfg[i].MapId);
		}
		size_t temp_size = tempVec.size();
		if (temp_size <= 0)	return false;

		SystemChatMsgData msgData;
		msgData.Clear();
		msgData.text.push_back(g_GetUnionMgr()->GetCharUnionName(createRsp.char_id()));
		msgData.text.push_back(pCfg->convoyName);
		msgData.params.push_back(tempVec[temp_size-1]);
		//公会xxx正在押送+先锋军物资/守备军物资（镖车等级）+，目的地xxx！
		g_GetChatCenter()->SendWorldBroadcastChat(BT_UNION_ESCORT_ESCORT_RUNNING, msgData);

		/*ProtoBuf::GCChatMsgRsp chatMsgRsp;
		chatMsgRsp.set_chattype(CHAT_TYPE_GUILD);
		ChatContentInfo *content = chatMsgRsp.add_chatcontent();
		content->set_describes("公会镖车已接取，请小伙伴火速前往护送！");
		g_GetChatCenter()->SendW2C(ProtoBuf::LOGIC_TO_CLIENT_CHATMSGREQ, iSet, &chatMsgRsp);*/

		SystemChatMsgData notify;
		g_GetChatCenter()->SendBroadcastChat(BT_UNION_RECV_ESCORT, notify, iSet);
	}
	return true;
}

bool EscortMgr::DestoryResult(RECV_PACKAGE &package)
{
	EscortDestoryRsp destoryRsp;
	if (!destoryRsp.ParseFromArray(package.dataAfterHead, package.sizeAfterHead))
	{
		LogErrFmtPrint("[center] EscortMgr::DestoryResult parse failed...");
		return false;
	}

	if (destoryRsp.destroy_type() != EscortOperate_Del_TaskSubmit)
	{
		Escort *pEscort = FindEscortByEsctId(destoryRsp.escort_cid());
		if (nullptr == pEscort)	return true;
		EscortData eData;
		eData.set_map_id(destoryRsp.map_id());
		eData.set_posx(destoryRsp.posx());
		eData.set_posy(destoryRsp.posy());
		eData.set_posz(destoryRsp.posz());
		NotifyComplete(pEscort, (EscortOperate)destoryRsp.destroy_type(), &eData);
		DeleteEscortByEsctId(pEscort->CID());
		pEscort->Complete(false);
		return true;
	}

	EscortSubmitRsp submitRsp;
	if (RET_SUCCESS != destoryRsp.retcode())
	{
		submitRsp.set_retcode(destoryRsp.retcode());
		return g_GetCenterService()->SendDataToClient(destoryRsp.char_id(), EMOUDLE_ID_ESCORT, SERVER_TO_CLIENT_ESCORT_SUBMIT, &submitRsp);
	}

	ActiveCharacter* pActiveCharacter = g_GetActiveCharacterMgr()->GetOnlineCharacter(destoryRsp.char_id());
	if (nullptr == pActiveCharacter)
	{
		submitRsp.set_retcode(RET_PLAYER_NOT_ONLINE);
		return g_GetCenterService()->SendDataToClient(destoryRsp.char_id(), EMOUDLE_ID_ESCORT, SERVER_TO_CLIENT_ESCORT_SUBMIT, &submitRsp);
	}

	Escort *pEscort = FindEscortByEsctId(destoryRsp.escort_cid());
	if (nullptr == pEscort)
	{
		submitRsp.set_retcode(RET_ESCORT_TASK_NOT_EXIST);
		return g_GetCenterService()->SendDataToClient(destoryRsp.char_id(), EMOUDLE_ID_ESCORT, SERVER_TO_CLIENT_ESCORT_SUBMIT, &submitRsp);
	}

	CUnion *pUnion = g_GetUnionMgr()->GetCharUnion(destoryRsp.char_id());
	if (nullptr == pUnion)
	{
		submitRsp.set_retcode(RET_UNION_NOT_EXISTS);
		return g_GetCenterService()->SendDataToClient(destoryRsp.char_id(), EMOUDLE_ID_ESCORT, SERVER_TO_CLIENT_ESCORT_SUBMIT, &submitRsp);
	}

	UnionChar *pUnionChar = g_GetUnionMgr()->CharUnionData(destoryRsp.char_id());
	if (nullptr == pUnionChar)
	{
		submitRsp.set_retcode(RET_UNION_NOT_JOIN);
		return g_GetCenterService()->SendDataToClient(destoryRsp.char_id(), EMOUDLE_ID_ESCORT, SERVER_TO_CLIENT_ESCORT_SUBMIT, &submitRsp);
	}

	const GuildescortEscortCfgInfo *gCfg = g_GetGuildescortEscortCfgTable()->GetGuildescortEscortCfgInfo(pEscort->EscortID());
	if (gCfg)
	{
		uint32_t addContri = ceil(gCfg->playerContributionBasicValue * destoryRsp.cur_hp_perct() / 100.f);
		g_GetUnionMgr()->AddCharCanUseContri(destoryRsp.char_id(), addContri, S_UNION_ESCORT);
		g_GetUnionMgr()->AddUnionMoney(destoryRsp.char_id(), gCfg->guildBasicReward);
	}

	if (EscortType_Guild == pEscort->Type())
	{
		SET_UINT64 iSet;
		g_GetUnionMgr()->GetUnionAllMember(pEscort->HostID(), iSet);
		SystemChatMsgData notify;
		g_GetChatCenter()->SendBroadcastChat(BT_UNION_ESCORT_REACH, notify, iSet);
	}

	EscortData eData;
	eData.set_map_id(destoryRsp.map_id());
	eData.set_posx(destoryRsp.posx());
	eData.set_posy(destoryRsp.posy());
	eData.set_posz(destoryRsp.posz());
	NotifyComplete(pEscort, EscortOperate_Del_TaskSubmit, &eData);
	DeleteEscortByEsctId(pEscort->CID());
	pEscort->Complete();

	submitRsp.set_retcode(RET_SUCCESS);
	g_GetCenterService()->SendDataToClient(destoryRsp.char_id(), EMOUDLE_ID_ESCORT, SERVER_TO_CLIENT_ESCORT_SUBMIT, &submitRsp);
	return true;
}

void EscortMgr::SpeedupResult(RECV_PACKAGE &package)
{
}

bool EscortMgr::RepairResult(RECV_PACKAGE &package)
{
	EscortResumeHpRsp resumeRsp;
	if (!resumeRsp.ParseFromArray(package.dataAfterHead, package.sizeAfterHead))
	{
		LogErrFmtPrint("[center] EscortMgr::RepairResult parse failed...");
		return false;
	}

	EscortRepairRsp repairRsp;
	if (resumeRsp.retcode() != RET_SUCCESS)
	{
		repairRsp.set_retcode(resumeRsp.retcode());
		return g_GetCenterService()->SendDataToClient(resumeRsp.char_id(), EMOUDLE_ID_ESCORT, SERVER_TO_CLIENT_ESCORT_REPAIR, &repairRsp);
	}

	Escort *pEscort = FindEscortByEsctId(resumeRsp.escort_cid());
	if (nullptr == pEscort)
	{
		LogErrFmtPrint("[center] EscortMgr::RepairResult Escort == nullptr failed...");
		repairRsp.set_retcode(RET_ESCORT_TASK_NOT_EXIST);
		return g_GetCenterService()->SendDataToClient(resumeRsp.char_id(), EMOUDLE_ID_ESCORT, SERVER_TO_CLIENT_ESCORT_REPAIR, &repairRsp);
	}

	const GuildescortEscortCfgInfo *pCfg = g_GetGuildescortEscortCfgTable()->GetGuildescortEscortCfgInfo(pEscort->EscortID());
	if (nullptr == pCfg)
	{
		LogErrFmtPrint("[center] EscortMgr::RepairResult GuildescortEscortCfgInfo == nullptr failed...");
		repairRsp.set_retcode(RET_ESCORT_CONFIG_ERROR);
		return g_GetCenterService()->SendDataToClient(resumeRsp.char_id(), EMOUDLE_ID_ESCORT, SERVER_TO_CLIENT_ESCORT_REPAIR, &repairRsp);
	}

	uint32_t contri = pCfg->vecGuildescortEscortFixCfg[resumeRsp.repair_index()].contribution;
	pEscort->AddContri(resumeRsp.char_id(), contri);
	pEscort->AddRepair(resumeRsp.char_id());

	EscortAddUnionContri addContri;
	addContri.set_value(contri);
	g_GetCenterService()->SendDataToClient(resumeRsp.char_id(), EMOUDLE_ID_ESCORT, SERVER_TO_CLIENT_ESCORT_ADD_UNION_CONTRI, &addContri);

	repairRsp.set_retcode(RET_SUCCESS);
	return g_GetCenterService()->SendDataToClient(resumeRsp.char_id(), EMOUDLE_ID_ESCORT, SERVER_TO_CLIENT_ESCORT_REPAIR, &repairRsp);
}

void EscortMgr::GetInfoResult(RECV_PACKAGE &package)
{
	L2C_EscortInfoRsp infoRsp;
	if (!infoRsp.ParseFromArray(package.dataAfterHead, package.sizeAfterHead))
	{
		LogErrFmtPrint("[center] EscortMgr::GetInfoResult parse failed...");
		return;
	}

	bool result = false;
	Escort *pEscort = nullptr;
	EscortData eData;
	ERetCode retcode = (ERetCode)infoRsp.retcode();

	if (retcode == RET_SUCCESS)
	{
		pEscort = FindEscortByEsctId(infoRsp.escort_cid());
		if (nullptr != pEscort)
		{
			eData.set_escort_cid(pEscort->CID());
			eData.set_escort_id(pEscort->EscortID());
			eData.set_path_id(pEscort->LineID());
			eData.set_repair_flag(pEscort->HaveRepair(infoRsp.char_id()));
			eData.set_time_tick(GetStartTick(pEscort));
			eData.set_map_id(infoRsp.map_id());
			eData.set_posx(infoRsp.posx());
			eData.set_posy(infoRsp.posy());
			eData.set_posz(infoRsp.posz());
			result = true;
		}
		else
		{
			retcode = RET_FAIL;
		}
	}
	
	if (infoRsp.drive() == EscortDrive_GetInfo)
	{
		EscortInfoRsp rsp;
		rsp.set_retcode(retcode);
		if (result)
		{
			rsp.mutable_escort_data()->CopyFrom(eData);
		}
		g_GetCenterService()->SendDataToClient(infoRsp.char_id(), EMOUDLE_ID_ESCORT, SERVER_TO_CLIENT_ESCORT_GET_INFO, &rsp);
	}
	else if (infoRsp.drive() == EscortDrive_GetCharInfo)
	{
		EscortCharInfoRsp rsp;
		rsp.set_retcode(RET_SUCCESS);
		rsp.set_char_times(GetCharEscortTimes(infoRsp.char_id()));
		rsp.set_union_times(GetGuildEscortTimes(infoRsp.char_id()));

		if (result)
		{
			rsp.mutable_escort_data()->CopyFrom(eData);
		}
		g_GetCenterService()->SendDataToClient(infoRsp.char_id(), EMOUDLE_ID_ESCORT, SERVER_TO_CLIENT_ESCORT_GET_CHAR_INFO, &rsp);
	}
	else if (infoRsp.drive() == EscortDrive_Server)
	{
		EscortNotifyInfo notify;
		notify.set_operate(EscortOperate_Add);
		notify.set_escort_cid(infoRsp.escort_cid());
		if (result)
		{
			notify.mutable_escort_data()->CopyFrom(eData);
		}
		g_GetCenterService()->SendDataToClient(infoRsp.char_id(), EMOUDLE_ID_ESCORT, SERVER_TO_CLIENT_ESCORT_NOTIFY, &notify);
	}
}

void EscortMgr::OnEscortDead(RECV_PACKAGE &package)
{
	EscortDead eDead;
	if (!eDead.ParseFromArray(package.dataAfterHead, package.sizeAfterHead))
	{
		LogErrFmtPrint("[center] EscortMgr::OnEscortDead parse failed...");
		return;
	}

	Escort *pEscort = FindEscortByEsctId(eDead.escort_cid());
	if (nullptr == pEscort)	return;

	EscortData eData;
	eData.set_map_id(eDead.map_id());
	eData.set_posx(eDead.posx());
	eData.set_posy(eDead.posy());
	eData.set_posz(eDead.posz());
	NotifyComplete(pEscort, EscortOperate_Del_Killed, &eData);
	DeleteEscortByEsctId(pEscort->CID());
	pEscort->Complete(false);
}

void EscortMgr::OnTransLogicServer(RECV_PACKAGE &package)
{
	L2C_EscortTrans trans;
	if (!trans.ParseFromArray(package.dataAfterHead, package.sizeAfterHead))
	{
		LogErrFmtPrint("[center] EscortMgr::OnTransLogicServer parse failed...");
		return;
	}

	LogInfoFmtPrint("[center] ++++++++++++++++++++OnTransLogicServer+++++++++++++++++++++++++");
	Escort *pEscort = FindEscortByEsctId(trans.old_escort_cid());
	if (nullptr == pEscort)		return;

	pEscort->CID(trans.new_escort_cid());
	pEscort->ServerID(package.session->GetUserData().v32);
	DeleteEscortByEsctId(trans.old_escort_cid());
	mEscortEsctIdMap.insert(std::make_pair(pEscort->CID(), pEscort));

	EscortData eData;
	eData.set_escort_cid(pEscort->CID());
	eData.set_escort_id(pEscort->EscortID());
	eData.set_path_id(pEscort->LineID());
	eData.set_time_tick(GetStartTick(pEscort));
	eData.set_map_id(trans.map_id());
	eData.set_posx(trans.posx());
	eData.set_posy(trans.posy());
	eData.set_posz(trans.posz());

	if (EscortType_Char == pEscort->Type())
	{
		EscortNotifyInfo notify;
		notify.set_operate(EscortOperate_Del_SwitchServer);
		notify.set_escort_cid(pEscort->CID());
		eData.set_repair_flag(pEscort->HaveRepair(pEscort->HostID()));
		notify.mutable_escort_data()->CopyFrom(eData);
		g_GetCenterService()->SendDataToClient(pEscort->HostID(), EMOUDLE_ID_ESCORT, SERVER_TO_CLIENT_ESCORT_NOTIFY, &notify);
	}
	else if (EscortType_Guild == pEscort->Type())
	{
		SET_UINT64 iSet;
		g_GetUnionMgr()->GetUnionAllMember(pEscort->HostID(), iSet);
		for (SET_UINT64::iterator iter = iSet.begin(); iter != iSet.end(); iter++)
		{
			EscortNotifyInfo notify;
			notify.set_operate(EscortOperate_Del_SwitchServer);
			notify.set_escort_cid(pEscort->CID());
			eData.set_repair_flag(pEscort->HaveRepair(*iter));
			notify.mutable_escort_data()->CopyFrom(eData);
			g_GetCenterService()->SendDataToClient(*iter, EMOUDLE_ID_ESCORT, SERVER_TO_CLIENT_ESCORT_NOTIFY, &notify);
		}
	}
}

void EscortMgr::OnEscortStopMove(RECV_PACKAGE &package)
{
	L2C_EscortStopMove stopMove;
	if (!stopMove.ParseFromArray(package.dataAfterHead, package.sizeAfterHead))
	{
		LogErrFmtPrint("[center] EscortMgr::OnEscortStopMove parse failed...");
		return;
	}
	Escort *pEscort = FindEscortByEsctId(stopMove.escort_cid());
	if (nullptr == pEscort)
	{
		LogErrFmtPrint("[center] EscortMgr::OnEscortStopMove pEscort == nullptr... failed");
		return;
	}

	CUnion *pUion = g_GetUnionMgr()->GetUnion(pEscort->HostID());
	if (nullptr == pUion)
	{
		LogErrFmtPrint("[center] EscortMgr::OnEscortStopMove pUion == nullptr... failed");
		return;
	}

	/*ProtoBuf::GCChatMsgRsp chatMsgRsp;
	chatMsgRsp.set_chattype(CHAT_TYPE_GUILD);
	ChatContentInfo *content = chatMsgRsp.add_chatcontent();
	content->set_describes("本公会所押送的军需物质在xxx处无人监管，请小伙伴们火速前往支援！");

	SET_UINT64 members;
	pUion->AllMember(members);
	g_GetChatCenter()->SendW2C(ProtoBuf::LOGIC_TO_CLIENT_CHATMSGREQ, members, &chatMsgRsp);*/

	SET_UINT64 members;
	pUion->AllMember(members);
	SystemChatMsgData notify;
	notify.params.push_back(stopMove.map_id());
	g_GetChatCenter()->SendBroadcastChat(BT_UNION_ESCORT_NO_CARE, notify, members);
}

void EscortMgr::OnEscortAddContri(RECV_PACKAGE &package)
{
	L2C_EscortAddContri addContri;
	if (!addContri.ParseFromArray(package.dataAfterHead, package.sizeAfterHead))
	{
		LogErrFmtPrint("[center] EscortMgr::OnEscortAddContri parse failed...");
		return;
	}
	Escort *pEscort = FindEscortByEsctId(addContri.escort_cid());
	if (nullptr == pEscort)
	{
		LogErrFmtPrint("[center] EscortMgr::OnEscortAddContri pEscort == nullptr... failed");
		return;
	}

	/*CUnion *pUion = g_GetUnionMgr()->GetUnion(addContri.char_id());
	if (nullptr == pUion)
	{
		LogErrFmtPrint("[center] EscortMgr::OnEscortAddContri pUion == nullptr... failed");
		return;
	}*/
	pEscort->AddContri(addContri.char_id(), addContri.escort_contri());

	EscortAddUnionContri add;
	add.set_value(addContri.escort_contri());
	g_GetCenterService()->SendDataToClient(addContri.char_id(), EMOUDLE_ID_ESCORT, SERVER_TO_CLIENT_ESCORT_ADD_UNION_CONTRI, &add);
}

void EscortMgr::OnEscortArrival(RECV_PACKAGE &package)
{
	L2C_EscortArrivalDest l2cArrival;
	if (!l2cArrival.ParseFromArray(package.dataAfterHead, package.sizeAfterHead))
	{
		LogErrFmtPrint("[center] EscortMgr::OnEscortArrival parse failed...");
		return;
	}

	Escort *pEscort = FindEscortByEsctId(l2cArrival.escort_cid());
	if (nullptr == pEscort)
	{
		LogErrFmtPrint("[center] EscortMgr::OnEscortArrival nullptr == pEscort failed...");
		return;
	}

	if (EscortType_Guild != pEscort->Type())
	{
		LogErrFmtPrint("[center] EscortMgr::OnEscortArrival escort type is not guild type...type:%d", pEscort->Type());
		return;
	}

	CUnion *pUnion = g_GetUnionMgr()->GetUnion(pEscort->HostID());
	if (nullptr == pUnion)
	{
		LogErrFmtPrint("[center] EscortMgr::OnEscortArrival nullptr == == pUnion failed...guildId:%llu", pEscort->HostID());
		return;
	}

	const GuildescortEscortCfgInfo *gCfg = g_GetGuildescortEscortCfgTable()->GetGuildescortEscortCfgInfo(pEscort->EscortID());
	if (gCfg)
	{
		pUnion->AddUnionMoney(gCfg->guildBasicReward/* + gCfg->foregift*/);
	}

	SET_UINT64 iSet;
	g_GetUnionMgr()->GetUnionAllMember(pEscort->HostID(), iSet);
	SystemChatMsgData notify;
	g_GetChatCenter()->SendBroadcastChat(BT_UNION_ESCORT_REACH, notify, iSet);
	
	pUnion->EscortTimes(pUnion->EscortTimes() + 1);

	EscortData eData;
	eData.set_map_id(l2cArrival.map_id());
	eData.set_posx(l2cArrival.posx());
	eData.set_posy(l2cArrival.posy());
	eData.set_posz(l2cArrival.posz());
	//NotifyComplete(pEscort, EscortOperate_Del_TaskSubmit, &eData);

	g_GetUnionMgr()->GetUnionAllMember(pEscort->HostID(), iSet);
	for (SET_UINT64::iterator iter = iSet.begin(); iter != iSet.end(); iter++)
	{
		EscortNotifyInfo notify;
		notify.set_operate(EscortOperate_Del_TaskSubmit);
		notify.set_escort_cid(pEscort->CID());
		notify.mutable_escort_data()->CopyFrom(eData);
		g_GetCenterService()->SendDataToClient(*iter, EMOUDLE_ID_ESCORT, SERVER_TO_CLIENT_ESCORT_NOTIFY, &notify);
	}

	DeleteEscortByEsctId(pEscort->CID());
	pEscort->Complete();
}

void EscortMgr::OnQuitUnion(CharIDType charId, CharIDType guildId)
{
	Escort *pEscort = FindEscortByGuildId(guildId);
	if (pEscort && pEscort->CID() > 0)
	{
		EscortNotifyInfo notify;
		notify.set_operate(EscortOperate_Del_QuitUnion);
		notify.set_escort_cid(pEscort->CID());
		g_GetCenterService()->SendDataToClient(charId, EMOUDLE_ID_ESCORT, SERVER_TO_CLIENT_ESCORT_NOTIFY, &notify);
	}

	Escort *pCharEscort = FindEscortByCharId(charId);
	if (pCharEscort && pCharEscort->CID() > 0)
	{
		EscortDestoryReq destory;
		destory.set_char_id(charId);
		destory.set_escort_cid(pCharEscort->CID());
		destory.set_destroy_type(EscortOperate_Del_QuitUnion);
		g_GetCenterService()->SendDataToLogicByCharID(charId, EMOUDLE_ID_ESCORT, CENTER_TO_LOGIC_INTERAL_ESCORT_DESTORY, &destory);
	}
}

void EscortMgr::OnJoinUnion(CharIDType charId, CharIDType guildId)
{
	Escort *pEscort = FindEscortByGuildId(guildId);
	if (nullptr == pEscort || pEscort->CID() <= 0)
	{
		return;
	}
	C2L_EscortInfoReq posreq;
	posreq.set_char_id(charId);
	posreq.set_escort_cid(pEscort->CID());
	posreq.set_drive(EscortDrive_Server);
	g_GetCenterService()->SendDataToLogic(pEscort->ServerID()/*镖车所在的逻辑服ID*/, EMOUDLE_ID_ESCORT, CENTER_TO_LOGIC_INTERAL_ESCORT_GET_INFO, &posreq);
}

void EscortMgr::OnDisbUnion(/*CharIDType charId, */CharIDType guildId)
{
	//删除场景镖车
	Escort *pEscort = FindEscortByGuildId(guildId);
	if (nullptr == pEscort || pEscort->CID() <= 0)	return;
	EscortDestoryReq destory;
	destory.set_escort_cid(pEscort->CID());
	destory.set_destroy_type(EscortOperate_Del_QuitUnion);
	g_GetCenterService()->SendDataToLogic(pEscort->ServerID(), EMOUDLE_ID_ESCORT, CENTER_TO_LOGIC_INTERAL_ESCORT_DESTORY, &destory);
}

void EscortMgr::OnRobberOff(RECV_PACKAGE &package)
{
	L2C_EscortRobberOff robber;
	if (!robber.ParseFromArray(package.dataAfterHead, package.sizeAfterHead))
	{
		LogErrFmtPrint("[center] EscortMgr::OnRobberOff parse failed...");
		return;
	}

	Escort *pEscort = FindEscortByEsctId(robber.escort_cid());
	if (nullptr == pEscort || pEscort->CID() <= 0)	return;

	pEscort->RobberStatus(EscortRobberStatus_None);
	EscortRobberNotify notify;
	notify.set_escort_cid(pEscort->HostID());
	notify.set_status(2);	//1.劫镖进行 2.劫镖停止
	g_GetCenterService()->SendDataToClient(pEscort->HostID(), EMOUDLE_ID_ESCORT, SERVER_TO_CLIENT_ESCORT_ROBBER_STATUS, &notify);

	const GuildescortRobberCfgInfo *pCfg = g_GetGuildescortRobberCfgTable()->GetGuildescortRobberCfgInfo(pEscort->RobberID());
	if (nullptr != pCfg)
	{
		pEscort->RobberTime(Random(pCfg->minRefreshTime, pCfg->maxRefreshTime));
	}
}

void EscortMgr::DailyUpdate()
{
	for (ESCORT_ESCTID_MAP::iterator it = mEscortEsctIdMap.begin(); it != mEscortEsctIdMap.end();/* it++*/)
	{
		Escort *pEscort = it->second;
		if (nullptr == pEscort || EscortType_Guild == pEscort->Type())
		{
			it++;
			continue;
		}
		if (pEscort->CID() <= 0)
		{
			DeleteEscortByCharId(pEscort->HostID());
			mEscortEsctIdMap.erase(it++);
			continue;
		}
		it++;
	}
}

void EscortMgr::WeekUpdate()
{
	for (ESCORT_ESCTID_MAP::iterator it = mEscortEsctIdMap.begin(); it != mEscortEsctIdMap.end();/* it++*/)
	{
		Escort *pEscort = it->second;
		if (nullptr == pEscort || EscortType_Char == pEscort->Type())
		{
			it++;
			continue;
		}
		if (pEscort->CID() <= 0)
		{
			DeleteEscortByGuildId(pEscort->HostID());
			mEscortEsctIdMap.erase(it++);
			continue;
		}
		it++;
	}
}

void EscortMgr::ActivityStop()
{
	//前线补给只控制公会镖
	for (ESCORT_ESCTID_MAP::iterator iter = mEscortEsctIdMap.begin(); iter != mEscortEsctIdMap.end(); ++iter)
	{
		Escort *pEscort = iter->second;
		if (nullptr == pEscort || pEscort->CID() <= 0 || pEscort->Type() == EscortType_Char)
			continue;

		EscortDestoryReq destory;
		destory.set_escort_cid(pEscort->CID());
		destory.set_destroy_type(EscortOperate_Del_Activity);
		g_GetCenterService()->SendDataToLogic(pEscort->ServerID(), EMOUDLE_ID_ESCORT, CENTER_TO_LOGIC_INTERAL_ESCORT_DESTORY, &destory);
	}
}

void EscortMgr::Update()
{
	for (ESCORT_ESCTID_MAP::iterator it = mEscortEsctIdMap.begin(); it != mEscortEsctIdMap.end(); ++it)
	{
		Escort *pEscort = it->second;
		if (nullptr == pEscort || pEscort->CID() <= 0)
		{
			continue;
		}
		if (UpdateTimeLimit(pEscort))
		{
			continue;
		}
		UpdateRobber(pEscort);
		UpdateSpeedTime(pEscort);
		UpdateQteTime(pEscort);
	}
}

bool EscortMgr::UpdateTimeLimit(Escort *e)
{
	if (e->TimeLast() <= 0)		return false;
	
	e->TimeLast(e->TimeLast() - 1);

	if (e->TimeLast() <= 0)
	{
		EscortDestoryReq destory;
		destory.set_escort_cid(e->CID());
		destory.set_destroy_type(EscortOperate_Del_TimeOver);
		g_GetCenterService()->SendDataToLogic(e->ServerID(), EMOUDLE_ID_ESCORT, CENTER_TO_LOGIC_INTERAL_ESCORT_DESTORY, &destory);
		return true;
	}
	return false;
}

void EscortMgr::UpdateRobber(Escort *e)
{
	if (e->RobberTime() <= 0)		return;

	//LogInfoFmtPrint("[center] +++++++++++++++++++UpdateRobber+++++++++++++++++time:%d", e->RobberTime());
	e->RobberTime(e->RobberTime() - 1);

	if (e->RobberTime() <= 0)
	{
		if (EscortSpeedStatus_Active == e->SpeedStatus())
		{
			e->RobberStatus(EscortRobberStatus_Ready);
			return;
		}
		e->RobberStatus(EscortRobberStatus_Active);
		C2L_EscortCreateRobber robber;
		robber.set_escort_cid(e->CID());
		robber.set_robber_id(e->RobberID());
		robber.set_robber_level(e->EscortLevel());
		g_GetCenterService()->SendDataToLogic(e->ServerID(), EMOUDLE_ID_ESCORT, CENTER_TO_LOGIC_INTERAL_ESCORT_ROBBER, &robber);

		EscortRobberNotify notify;
		notify.set_escort_cid(e->HostID());
		notify.set_status(1);	//1.劫镖进行 2.劫镖停止
		g_GetCenterService()->SendDataToClient(e->HostID(), EMOUDLE_ID_ESCORT, SERVER_TO_CLIENT_ESCORT_ROBBER_STATUS, &notify);
	}
}

void EscortMgr::UpdateSpeedTime(Escort *e)
{
	if (e->SpeedTime() <= 0)	return;

	e->SpeedTime(e->SpeedTime() - 1);

	if (e->SpeedTime() <= 0)
	{
		EscortSpeedup speedup;
		speedup.set_escort_cid(e->CID());
		speedup.set_speed_value(e->Speed());
		g_GetCenterService()->SendDataToLogic(e->ServerID(), EMOUDLE_ID_ESCORT, CENTER_TO_LOGIC_INTERAL_ESCORT_SPEEDUP, &speedup);
	}
}

void EscortMgr::UpdateQteTime(Escort *e)
{
	if (e->QteTime() <= 0)		return;

	e->QteTime(e->QteTime() - 1);

	if (e->QteTime() <= 0)
	{
		if (EscortRobberStatus_Ready == e->RobberStatus())
		{
			e->RobberStatus(EscortRobberStatus_Active);
			C2L_EscortCreateRobber robber;
			robber.set_escort_cid(e->CID());
			robber.set_robber_id(e->RobberID());
			robber.set_robber_level(e->EscortLevel());
			g_GetCenterService()->SendDataToLogic(e->ServerID(), EMOUDLE_ID_ESCORT, CENTER_TO_LOGIC_INTERAL_ESCORT_ROBBER, &robber);

			EscortRobberNotify notify;
			notify.set_escort_cid(e->CID());
			notify.set_status(1);	//1.劫镖进行 2.劫镖停止
			g_GetCenterService()->SendDataToClient(e->HostID(), EMOUDLE_ID_ESCORT, SERVER_TO_CLIENT_ESCORT_ROBBER_STATUS, &notify);
		}
	}
}

void EscortMgr::SendEmail(CharIDType charId, uint32_t contri)
{
	/*string strTopic = "军需物资护送奖励";
	char text1[] = { 0 };
	//char text2[] = { 0 };
	snprintf(text1, sizeof(text1), "本次公会承接的阵营军需物资已顺利送达目的地，阵营因为我们的努力又强大了一些！活动中你一共获得了%d点押镖贡献度，已转化为%d点公会贡献（已发放），附件中的奖励都是你的努力换来的，感谢一路有你！\n附录1，本次活动押镖贡献度前十成员", 99, 99);
	std::vector<ItemProtoInfo> v;
	g_GetMailMgr()->SendSystemMail(charId, strTopic, text1, 0, 0, 0, 0, 0, v);*/
}

void EscortMgr::NotifyComplete(Escort *pEscort, EscortOperate opetate/*, uint32_t param*/, EscortData *eData)
{
	string mailTitle = "物资护送失败";
	string mailContent = (opetate == EscortOperate_Del_Activity) ? "由于活动时间结束，护送的军需物资未能送达目的地，很遗憾，任务失败！" : "物资车完全损毁，任务失败，物资车的押金不再返还";
	VEC_ITEM_PROTO_EX vec;
	if (pEscort->Type() == EscortType_Char)
	{
		EscortNotifyInfo notify;
		notify.set_operate(opetate);
		notify.set_escort_cid(pEscort->CID());
		if (eData)
			notify.mutable_escort_data()->CopyFrom(*eData);
		g_GetCenterService()->SendDataToClient(pEscort->HostID(), EMOUDLE_ID_ESCORT, SERVER_TO_CLIENT_ESCORT_NOTIFY, &notify);
		if (opetate != EscortOperate_Del_TaskSubmit)
			g_GetMailMgr()->SendSystemMail(pEscort->HostID(), mailTitle, mailContent, 0, 0, 0, 0, 0, vec);
	}
	else if (pEscort->Type() == EscortType_Guild)
	{
		SET_UINT64 iSet;
		g_GetUnionMgr()->GetUnionAllMember(pEscort->HostID(), iSet);
		for (SET_UINT64::iterator iter = iSet.begin(); iter != iSet.end(); iter++)
		{
			EscortNotifyInfo notify;
			notify.set_operate(opetate);
			notify.set_escort_cid(pEscort->CID());
			if (eData)
				notify.mutable_escort_data()->CopyFrom(*eData);
			g_GetCenterService()->SendDataToClient(*iter, EMOUDLE_ID_ESCORT, SERVER_TO_CLIENT_ESCORT_NOTIFY, &notify);
			if (opetate != EscortOperate_Del_TaskSubmit)
				g_GetMailMgr()->SendSystemMail(*iter, mailTitle, mailContent, 0, 0, 0, 0, 0, vec);
		}

		if (opetate == EscortOperate_Del_TimeOver)
		{
			SystemChatMsgData notify;
			g_GetChatCenter()->SendBroadcastChat(BT_UNION_ESCORT_NOT_REACH, notify, iSet);
		}
		else if (opetate == EscortOperate_Del_Killed)
		{
			SystemChatMsgData notify;
			notify.params.push_back(eData != nullptr ? eData->map_id() : 0);
			g_GetChatCenter()->SendBroadcastChat(BT_UNION_ESCORT_DESTORY, notify, iSet);
		}
		else if (opetate == EscortOperate_Del_Activity)
		{
			SystemChatMsgData notify;
			g_GetChatCenter()->SendBroadcastChat(BT_UNION_ESCORT_ACTIVITY_FAIL, notify, iSet);
		}
	}
	//DeleteEscortByEsctId(pEscort->CID());
}

Escort* EscortMgr::MakeEscortInfoObj()
{
	if (nullptr == m_pEscortInfoPool)
	{
		return nullptr;
	}
	return m_pEscortInfoPool->MallocObj();
}

bool EscortMgr::FreeEscortInfoObj(Escort *pObj)
{
	if (nullptr == pObj)
	{
		return false;
	}
	return m_pEscortInfoPool->FreeObj(pObj);
}

bool EscortMgr::HasCharEscort(CharIDType charId)
{
	ESCORT_CHARID_MAP::iterator iter = mEscortCharIdMap.find(charId);
	if (iter != mEscortCharIdMap.end())
	{
		return iter->second->CID() > 0;
	}
	return false;
}

bool EscortMgr::HasGuildEscort(CharIDType guildId)
{
	ESCORT_GUILDID_MAP::iterator iter = mEscortGuildIdMap.find(guildId);
	if (iter != mEscortGuildIdMap.end())
	{
		return iter->second->CID() > 0;
	}
	return false;
}

bool EscortMgr::GuilderHasEscort(CharIDType guildId)
{
	SET_UINT64 iSet;
	g_GetUnionMgr()->GetUnionAllMember(guildId, iSet);
	for (SET_UINT64::iterator iter = iSet.begin(); iter != iSet.end(); iter++)
	{
		//if (HasCharEscort(*iter))
		if (FindActiveEscortCId(*iter))
		{
			return true;
		}
	}
	return false;
}

Escort* EscortMgr::FindActiveEscortCId(CharIDType charId)
{
	Escort* charEscort = FindEscortByCharId(charId);
	CharIDType guildId = g_GetUnionMgr()->GetCharUnionId(charId);
	Escort* guildEscort = FindEscortByGuildId(guildId);
	if (charEscort && charEscort->CID() > 0 && guildEscort && guildEscort->CID() > 0)
	{
		LogErrFmtPrint("[center] EscortMgr::FindActiveEscortCId both charEscort and  guildEscort exist..... serious error...");
	}
	if (charEscort && charEscort->CID() > 0)
	{
		return charEscort;
	}
	if (guildEscort && guildEscort->CID() > 0)
	{
		return guildEscort;
	}
	return nullptr;
}

uint32_t EscortMgr::GetCharEscortTimes(CharIDType charId)
{
	UnionChar *pCharUnion = g_GetUnionMgr()->CharUnionData(charId);
	if (pCharUnion)
	{
		return pCharUnion->EscortTimes();
	}

	return 0;
}

uint32_t EscortMgr::GetGuildEscortTimes(CharIDType charId)
{
	CUnion *pUnion = g_GetUnionMgr()->GetCharUnion(charId);
	if (pUnion)
	{
		return pUnion->EscortTimes();
	}

	return 0;
}

Escort* EscortMgr::FindEscortByCharId(CharIDType charId)
{
	ESCORT_CHARID_MAP::iterator iter = mEscortCharIdMap.find(charId);
	return iter != mEscortCharIdMap.end() ? iter->second : nullptr;
}

Escort* EscortMgr::NewEscortFromCharMap(CharIDType charId)
{
	Escort* newEscort = MakeEscortInfoObj();
	if (nullptr == newEscort)	return nullptr;
	newEscort->CfgTimes(_char_times_limit);
	mEscortCharIdMap.insert(make_pair(charId, newEscort));
	return mEscortCharIdMap[charId];
}

Escort* EscortMgr::MulEscortFromCharMap(CharIDType charId)
{
	if (FindEscortByCharId(charId))	return mEscortCharIdMap[charId];
	return NewEscortFromCharMap(charId);
}

void EscortMgr::DeleteEscortByCharId(CharIDType charId)
{
	ESCORT_CHARID_MAP::iterator it = mEscortCharIdMap.find(charId);
	if (it != mEscortCharIdMap.end())
	{
		//DeleteEscortByEsctId(it->second->CID());
		FreeEscortInfoObj(it->second);
		mEscortCharIdMap.erase(it);
	}
}

Escort* EscortMgr::FindEscortByGuildId(CharIDType guildId)
{
	ESCORT_GUILDID_MAP::iterator iter = mEscortGuildIdMap.find(guildId);
	return iter != mEscortGuildIdMap.end() ? iter->second : nullptr;
}

Escort* EscortMgr::NewEscortFromGuildMap(CharIDType guildId)
{
	Escort* newEscort = MakeEscortInfoObj();
	if (nullptr == newEscort)	return nullptr;
	newEscort->CfgTimes(_guild_times_limit);
	mEscortGuildIdMap.insert(make_pair(guildId, newEscort));
	return mEscortGuildIdMap[guildId];
}

Escort* EscortMgr::MulEscortFromGuildMap(CharIDType guildId)
{
	if (FindEscortByGuildId(guildId))	return mEscortGuildIdMap[guildId];
	return NewEscortFromGuildMap(guildId);
}

void EscortMgr::DeleteEscortByGuildId(CharIDType guildId)
{
	ESCORT_GUILDID_MAP::iterator it = mEscortGuildIdMap.find(guildId);
	if (it != mEscortGuildIdMap.end())	
	{
		//DeleteEscortByEsctId(it->second->CID());
		FreeEscortInfoObj(it->second);
		mEscortGuildIdMap.erase(guildId);
	}
}

Escort* EscortMgr::FindEscortByEsctId(CharIDType cid)
{
	ESCORT_ESCTID_MAP::iterator iter = mEscortEsctIdMap.find(cid);
	return iter != mEscortEsctIdMap.end() ? iter->second : nullptr;
}

void EscortMgr::DeleteEscortByEsctId(CharIDType cid)
{
	ESCORT_ESCTID_MAP::iterator it = mEscortEsctIdMap.find(cid);
	if (it != mEscortEsctIdMap.end())	mEscortEsctIdMap.erase(it);
}
